#coding:utf-8
'''
@author:        laohu
@date:          2021.07.14
@brief:         自动消失提示框
@note:          移植自QtOpenUtils
'''


from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
from UIComponents.Common.common import *


class QToast(QLabel):

    def __init__(self, parent, message, horizontalMargin, verticalMargin, style):
        super().__init__(parent)
        self.setAttribute(Qt.WA_TransparentForMouseEvents, True)
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.Tool)
        label = QLabel(self)
        layout = QHBoxLayout(self)
        layout.addWidget(label)
        label.setText(message)
        label.setStyleSheet(style)
        label.setAlignment(Qt.AlignCenter)
        label.adjustSize()
        label.setContentsMargins(horizontalMargin, verticalMargin, horizontalMargin, verticalMargin)
        self.adjustSize()
        self.startx = parent.x() + (parent.width() - self.width()) / 2 + (self.width() - label.width()) / 2
        self.starty = parent.y() + parent.height()
        self.endy = parent.y() + parent.height() * 3 / 4
        setShadow(label, 10.0, 0, 0)

    def toast(self):
        self.animal = QPropertyAnimation(self, QByteArray("pos"))
        self.animal.setDuration(1000)
        self.animal.setStartValue(QPoint(self.startx, self.starty))
        self.animal.setEndValue(QPoint(self.startx, self.endy))
        self.animal.setEasingCurve(QEasingCurve.OutCubic)
        self.animal.finished.connect(lambda:self.animationFinished())
        self.show()
        self.animal.start()

    def animationFinished(self):
        QTimer.singleShot(500, self, self.deleteLater())